package com.ruoyi.common.service.impl;


import com.ruoyi.common.utils.MinioClientUtil;
import com.ruoyi.common.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private MinioClientUtil minioClientUtil;

    @Override
    public String uploadMinio(MultipartFile file, String type) throws Exception {
        if (StringUtils.isEmpty(type)) {
            type = "system";
        }
        String path = type + "/" + file.getOriginalFilename();
        return minioClientUtil.putObject(path, file.getInputStream());
    }

    @Override
    public void download(HttpServletResponse response, String name, String type) {
        if (StringUtils.isEmpty(name)) {
            return;
        }
        if (StringUtils.isEmpty(type)) {
            type = "";
        }
        minioClientUtil.getObject(type, name, response);
    }

    @Override
    public Boolean delete(String name, String type) throws Exception {
        if (StringUtils.isEmpty(name)) {
            return Boolean.FALSE;
        }
        if (StringUtils.isEmpty(type)) {
            type = "system";
        }
        return minioClientUtil.removeObject(type + "/" + name);
    }

}
